package com.bsj.travel.process.config;

import com.bsj.travel.enums.MqttEventEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Objects;

/**
 * @author yinhao
 * @version 1.0
 * @description 通用主题
 * @date 2024/1/17
 */
@Slf4j
@RefreshScope
@Configuration
@ConfigurationProperties("topic.mqtt.public")
@Data
public class PublicTopicConfig {
    private List<String> shadow;
    private List<String> ota;
    private List<String> ntp;
    private List<String> attribute;
    private List<String> setattr;
    private List<String> setevent;

    public String getPubTopic(String pub, String productKey, String deviceName) {
        String topic = "";
        MqttEventEnum eventEnum = MqttEventEnum.getMqttEventEnum(pub);
        switch (Objects.requireNonNull(eventEnum)) {
            case OTA:
                topic = this.ota.get(1);
                break;
            case SHADOW:
                topic = this.shadow.get(1);
                break;
            case NTP:
                topic = this.ntp.get(1);
                break;
            case ATTRIBUTE:
                topic = this.attribute.get(1);
                break;
            case SETATTR:
                topic = this.setattr.get(0);
                break;
            case SETEVENT:
                topic = this.setevent.get(1);
                break;
            default:
                break;
        }
        String newTopic = topic.replaceAll("\\$\\{productKey\\}", productKey)
                .replaceAll("\\$\\{deviceName\\}", deviceName);
        //TODO 如果是事件上报，回复的主题还需做多一步处理
        return newTopic;
    }
}
